<?php
/**
 * 查询视频处理状态
 */
require_once dirname(__DIR__)."/login_filter.php";
//require_once dirname(dirname(__DIR__))."/common.php";
use app\cls\tom\Response;
use app\cls\tom\SysResource;
use app\cls\tom\Tools;
use app\cls\sdk\FoundaoVideoSDK;
use app\cls\tom\FFmpeg;
use app\cls\tom\Config;
use app\model\UserVideo;

if(empty($_REQUEST['job_id']) || empty($_REQUEST['move_name']) || empty($_REQUEST['video_url']) || empty($_REQUEST['guanlian_img_url'])){
    Response::output(-1,"缺少参数");
}
$FoundaoVideoSDK=new FoundaoVideoSDK();
$job = $FoundaoVideoSDK->getJobStatus($_REQUEST['job_id']);
if($job['status']==0){
    Response::output(-1,"未处理",$job);
}elseif($job['status']==1){
    Response::output(-1,"处理中",$job);
}elseif($job['status']==2){
    if(empty($job['output']) || empty($job['output'][0]['http_path'])){
        Response::output(-2,"处理失败1");
    }
    $video_url=$job['output'][0]['http_path'];
    //获取视频宽，高，时长
    $ffmpeg = new FFmpeg();
    $wh=$ffmpeg->getVideoDetail($video_url,4);
    //截取封面
    $img_path=upload_dir();
    $re=$ffmpeg->getCoverFromVideo1($video_url,$img_path['savepath']);
    sleep(1);
    //这是非编后的视频地址
    $path1=str_replace("//", "/", $job['output'][0]['path']);
    //下面把没转码的视频移动到指定目录
    $cmd="mv {$path1} {$_REQUEST['move_name']}";
    exec($cmd);
    //拼接数据库数据
    $data['height2']=$wh['height'];
    $data['width2']=$wh['width'];
    $data['duration']=$wh['duration'];
    $data['pic']=$img_path['savehttp'];
//    $data['mix_status']=1;
//    $data['video_url']=$video_9_16['savehttp'];
    //
    $db = SysResource::getDB(true);
    $UserVideo=new UserVideo($db);
    $res=$UserVideo->update_material($data,$_REQUEST['job_id']);
    if($res){
        $img_path['guanlian_img_url']=$_REQUEST['guanlian_img_url'];
        $img_path['job_id']=$_REQUEST['job_id'];
        $img_path['video_url']=$_REQUEST['video_url'];
        Response::output(0,"处理成功",$img_path,$_REQUEST['video_url']);
    }else{
        Response::output(-2,"处理失败2");
    }
//    Response::output(0,"处理成功",array_merge($img_path,$job));
}elseif($job['status']==-1){
    Response::output(-2,"处理失败3",$job);
}

function getMsecTime()
{
    list($msec, $sec) = explode(' ', microtime());
    $msectime =  (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
    return $msectime;
}
function upload_dir($type="image",$suffix="jpg"){
    //解决跨域问题
    $allUploadConfig = Config::load("cos_upload");

    $uploadConfig = $allUploadConfig[$type];

    //创建上传目录，配置上传
    $date1=date("Y",time());
    $date2=date("m",time());
    $date3=date("d",time());
    $dir="/".$date1."/".$date2."/".$date3;
    $uploadpath1=$uploadConfig['savepath'];
    $uploadpath2=$uploadConfig['savehttp'];
    $key1=rand(1000,10000);
    $key2=rand(10000,20000);
    $key3=rand();
    $file_name=$key1.$key2.$key3;
    //这是服务器上的上传地址
    $uploadpath['savepath']=$uploadpath1.$dir."/".$file_name.".{$suffix}";
    if($type!="image"){
        mkdir($uploadpath1.$dir,0777,true);
    }
    //这是网络访问地址
    $uploadpath['savehttp']=$uploadpath2.$dir."/".$file_name.".{$suffix}";
    return $uploadpath;
}



